package com.it.a01mymap;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class A04_HashMapDemo1 {
    public static void main(String[] args) {
        /*
            需求：创建一个HashMap集合，键是学生对象，值是籍贯
            存储三个键值对元素，并遍历
            要求：同姓名，同年龄认为是同一个学生
         */

        //创建HashMap集合
        HashMap<Student,String> hm = new HashMap<>();

        //创建学生对象
        Student s1 = new Student("John", 23);
        Student s2 = new Student("lisan", 24);
        Student s3 = new Student("John", 25);

        //添加到集合中
        hm.put(s1,"南昌");
        hm.put(s2,"长沙");
        hm.put(s3,"金华");

        //第一种遍历方式：通过键去找值,增强for进行遍历
        Set<Student> keys = hm.keySet();
        for (Student key : keys) {
            String value = hm.get(key);
            System.out.println(key+"="+value);
        }


        //第二种种遍历方式，entrySet获取到键值对对象
        Set<Map.Entry<Student, String>> entries = hm.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"="+value);
        }

        //lambda表达式
        hm.forEach((Student key, String value)-> System.out.println(key+"="+value));
    }
}
